#include "draw.h"

#define TETRA 1.0//(1.0/(2*sqrt(3.0/4.0)))

namespace draw
{
	static Vector3 icosohedronTriangles[] = {
		/*
		Vector3(TETRA, TETRA, TETRA),
		Vector3(-TETRA, TETRA, -TETRA),
		Vector3(-TETRA, -TETRA, TETRA),

		Vector3(-TETRA, -TETRA, TETRA),
		Vector3(TETRA, -TETRA, -TETRA),
		Vector3(TETRA, TETRA, TETRA),

		Vector3(-TETRA, TETRA, -TETRA),
		Vector3(TETRA, TETRA, TETRA),
		Vector3(TETRA, -TETRA, -TETRA),

		Vector3(-TETRA, TETRA, -TETRA),
		Vector3(TETRA, -TETRA, -TETRA),
		Vector3(-TETRA, -TETRA, TETRA),*/

		Vector3(0.0, 0.850650808352039932, 0.525731112119133606),
		Vector3(-0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(-0.525731112119133606, 0.0, 0.850650808352039932),

		Vector3(0.0, -0.850650808352039932, -0.525731112119133606),
		Vector3(0.850650808352039932, -0.525731112119133606, 0.0),
		Vector3(0.0, -0.850650808352039932, 0.525731112119133606),

		Vector3(0.0, -0.850650808352039932, -0.525731112119133606),
		Vector3(0.0, -0.850650808352039932, 0.525731112119133606),
		Vector3(-0.850650808352039932, -0.525731112119133606, 0.0),

		Vector3(-0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(0.0, -0.850650808352039932, -0.525731112119133606),
		Vector3(-0.850650808352039932, -0.525731112119133606, 0.0),

		Vector3(-0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(-0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(-0.850650808352039932, -0.525731112119133606, 0.0),

		Vector3(-0.525731112119133606, 0.0, 0.850650808352039932),
		Vector3(-0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(-0.850650808352039932, -0.525731112119133606, 0.0),

		Vector3(0.0, -0.850650808352039932, 0.525731112119133606),
		Vector3(-0.525731112119133606, 0.0, 0.850650808352039932),
		Vector3(-0.850650808352039932, -0.525731112119133606, 0.0),

		Vector3(-0.525731112119133606, 0.0, 0.850650808352039932),
		Vector3(0.0, -0.850650808352039932, 0.525731112119133606),
		Vector3(0.525731112119133606, 0.0, 0.850650808352039932),

		Vector3(0.0, 0.850650808352039932, 0.525731112119133606),
		Vector3(-0.525731112119133606, 0.0, 0.850650808352039932),
		Vector3(0.525731112119133606, 0.0, 0.850650808352039932),

		Vector3(0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.0, 0.850650808352039932, 0.525731112119133606),
		Vector3(0.525731112119133606, 0.0, 0.850650808352039932),

		Vector3(0.850650808352039932, -0.525731112119133606, 0.0),
		Vector3(0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.525731112119133606, 0.0, 0.850650808352039932),

		Vector3(0.0, -0.850650808352039932, 0.525731112119133606),
		Vector3(0.850650808352039932, -0.525731112119133606, 0.0),
		Vector3(0.525731112119133606, 0.0, 0.850650808352039932),

		Vector3(0.0, 0.850650808352039932, 0.525731112119133606),
		Vector3(0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.0, 0.850650808352039932, -0.525731112119133606),

		Vector3(-0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.0, 0.850650808352039932, 0.525731112119133606),
		Vector3(0.0, 0.850650808352039932, -0.525731112119133606),

		Vector3(-0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(-0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.0, 0.850650808352039932, -0.525731112119133606),

		Vector3(0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(-0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(0.0, 0.850650808352039932, -0.525731112119133606),

		Vector3(0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(0.0, 0.850650808352039932, -0.525731112119133606),

		Vector3(0.0, -0.850650808352039932, -0.525731112119133606),
		Vector3(-0.525731112119133606, 0.0, -0.850650808352039932),
		Vector3(0.525731112119133606, 0.0, -0.850650808352039932),

		Vector3(0.850650808352039932, -0.525731112119133606, 0.0),
		Vector3(0.0, -0.850650808352039932, -0.525731112119133606),
		Vector3(0.525731112119133606, 0.0, -0.850650808352039932),

		Vector3(0.850650808352039932, 0.525731112119133606, 0.0),
		Vector3(0.850650808352039932, -0.525731112119133606, 0.0),
		Vector3(0.525731112119133606, 0.0, -0.850650808352039932),
	};

	#define icosohedronCount (sizeof(icosohedronTriangles)/sizeof(icosohedronTriangles[0])/3)

	void triangleSmoothLevel(Vector3* vertices, int level) {
		if (!level)
		{
			n3d(vertices[0]);
			v3d(vertices[0]);
			n3d(vertices[1]);
			v3d(vertices[1]);
			n3d(vertices[2]);
			v3d(vertices[2]);
		}
		else
		{
			Vector3 subTriangles[4][3];

			//Create the center triangle

			subTriangles[0][0] = (vertices[0] + vertices[1]).normalize();
			subTriangles[0][1] = (vertices[1] + vertices[2]).normalize();
			subTriangles[0][2] = (vertices[2] + vertices[0]).normalize();

			//Place remaining triangles in counterclockwise order
			subTriangles[1][0] = vertices[0];
			subTriangles[1][1] = subTriangles[0][0];
			subTriangles[1][2] = subTriangles[0][2];

			subTriangles[2][0] = vertices[1];
			subTriangles[2][1] = subTriangles[0][1];
			subTriangles[2][2] = subTriangles[0][0];

			subTriangles[3][0] = vertices[2];
			subTriangles[3][1] = subTriangles[0][2];
			subTriangles[3][2] = subTriangles[0][1];

			//Draw each sub triangle
			for (int i = 0; i < 4; i++)
				triangleSmoothLevel(subTriangles[i], level-1);
		}
	}

	void sphere(int level) {
		glBegin(GL_TRIANGLES);
		for (unsigned int i = 0; i < icosohedronCount; i++)
			triangleSmoothLevel(icosohedronTriangles+i*3, level);
		glEnd();
	}
}
